package com.hz.module.system.controller.admin.check;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.hz.framework.common.pojo.PageParam;
import com.hz.framework.common.pojo.PageResult;
import com.hz.framework.common.pojo.CommonResult;
import com.hz.framework.common.util.object.BeanUtils;
import static com.hz.framework.common.pojo.CommonResult.success;

import com.hz.framework.excel.core.util.ExcelUtils;

import com.hz.framework.apilog.core.annotation.ApiAccessLog;
import static com.hz.framework.apilog.core.enums.OperateTypeEnum.*;

import com.hz.module.system.controller.admin.check.item.vo.*;
import com.hz.module.system.dal.dataobject.check.item.CheckItemDO;
import com.hz.module.system.service.check.item.CheckItemService;

@Tag(name = "管理后台 - 基础检查项")
@RestController
@RequestMapping("/system/check-item")
@Validated
public class CheckItemController {

    @Resource
    private CheckItemService checkItemService;

    @PostMapping("/create")
    @Operation(summary = "创建基础检查项")
    @PreAuthorize("@ss.hasPermission('system:check-item:create')")
    public CommonResult<String> createCheckItem(@Valid @RequestBody CheckItemSaveReqVO createReqVO) {
        return success(checkItemService.createCheckItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新基础检查项")
    @PreAuthorize("@ss.hasPermission('system:check-item:update')")
    public CommonResult<Boolean> updateCheckItem(@Valid @RequestBody CheckItemSaveReqVO updateReqVO) {
        checkItemService.updateCheckItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除基础检查项")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:check-item:delete')")
    public CommonResult<Boolean> deleteCheckItem(@RequestParam("id") String id) {
        checkItemService.deleteCheckItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得基础检查项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:check-item:query')")
    public CommonResult<CheckItemRespVO> getCheckItem(@RequestParam("id") String id) {
        CheckItemDO checkItem = checkItemService.getCheckItem(id);
        return success(BeanUtils.toBean(checkItem, CheckItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得基础检查项分页")
    @PreAuthorize("@ss.hasPermission('system:check-item:query')")
    public CommonResult<PageResult<CheckItemRespVO>> getCheckItemPage(@Valid CheckItemPageReqVO pageReqVO) {
        PageResult<CheckItemDO> pageResult = checkItemService.getCheckItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CheckItemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出基础检查项 Excel")
    @PreAuthorize("@ss.hasPermission('system:check-item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCheckItemExcel(@Valid CheckItemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CheckItemDO> list = checkItemService.getCheckItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "基础检查项.xls", "数据", CheckItemRespVO.class,
                        BeanUtils.toBean(list, CheckItemRespVO.class));
    }

}